<?php
/**
 * Created by PhpStorm.
 * User: ITS Lab
 * Date: 2015/9/26
 * Time: 0:08
 */
namespace Home\Controller;
use Think\Controller;

class OAuthController extends Controller
{

    public function callback()//OAuth回调函数
    {
        header("Content-type: text/html; charset=utf-8");
        $code=$_GET['code'];
        $state=$_GET['state'];
        $client_id=16;
        //$redirect_uri="http://localhost/LostAndFound/index.php/Home/OAuth/callback";
       $redirect_uri="http://".$_SERVER['SERVER_NAME'].U('OAuth/callback','');
        $client_secret="vJNmUZM366f41RXw09BQQPN2UK2mklDX";
        $url = "http://hometown.scau.edu.cn/open/OAuth/access_token";
        $post_data = array ("client_id"=>$client_id,"code"=>$code,"client_secret"=>$client_secret,"redirect_uri"=>$redirect_uri,"grant_type"=>"authorization_code");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        $json=json_decode($output);
        $uid=$json->{'uid'};
       // echo $uid;
      //  exit();
        $access_token=$json->{'access_token'};
        $url="http://hometown.scau.edu.cn/bbs/plugin.php?id=iltc_open:userinfo&uid=".$uid;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        //print_r($output);
        $info=json_decode($output,ture);
       // var_dump($info);
        $m=M("user");
        $data['bbs_uid']=$info['data']['uid'];
       $uid=$m->where($data['bbs_uid'])->find();
        $data['update_time']=date('y-m-d H:i:s',time());
        $data['ip']=get_client_ip();
        $where['bbs_uid']=$data['bbs_uid'];
           if($uid==null)
             $m->add($data);
        else
            $m->where($where)->save($data);
       // $_SESSION['uid']=$info['data']['uid'];
        session('uid',$info['data']['uid']);
        session('username',$info['data']['username']);
       //echo $_SESSION['username'];
        $username=$info["data"]["username"];
        if(isset($_SESSION['href'])) {
            $href=$_SESSION['href'];
            if(isset($_SESSION['thing_id']))
            {
                $id=$_SESSION['thing_id'];
            $this->redirect("$href?id=$id");
            }
            else  $this->redirect("$href");
        }
    }
    public function Login()//登录接入论坛登录
    {
        $CLIENT_ID=16;
        $SCOPE=NULL;
       // $REDIRECT_URI="http://localhost/LostAndFound/index.php/Home/OAuth/callback";
        $REDIRECT_URI="http://".$_SERVER['SERVER_NAME'].U('OAuth/callback','');
       // define('CLIENT_ID',16);
       // define('SCOPE',NULL);
        //define('REDIRECT_URI','http://localhost/LostandFound/index.php/Home/OAuth/callback');
         $state=md5(rand(1,10));
        $url="http://hometown.scau.edu.cn/open/OAuth/authorize?client_id=$CLIENT_ID&response_type=code&redirect_uri=$REDIRECT_URI&state=$state&scope=$SCOPE";
echo     $REDIRECT_URI;  //exit();
       header("location:$url");
       // $this->display();
    }
      function _empty(){
        header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
        $this->display("Error:error404");
    }
}